require 'dict'
require 'utils'

function main() 

	-- load dic
	print('load dic')
	local f = torch.DiskFile(dic_emb_path, 'r')
	local dic = f:readObject()
	setmetatable(dic, Dict_mt)
	f:close()
	
	-- process
	print('load data')
	local data = {}

	for line in io.lines(context_path) do
		local toks = split_string(line)

		for i,tok in ipairs(toks) do
			if tok:sub(1,1) == "_" and tok:sub(-1,-1) == "_" then
				local datum = torch.IntTensor(1 + 2*window_size)
				toks[i] = tok:sub(2,-2)

				for j = i-window_size,i+window_size do
					local id = j - (i-window_size) + 1
					if j < 1 then 
						datum[{id}] = dic.word2id['$START$']
					elseif j > #toks then
						datum[{id}] = dic.word2id['$END$']
					else
						if tonumber(toks[j]) ~= nil then
							toks[j] = '0'
						end
						local wid = dic.word2id[toks[j]:lower()]
						if wid == nil then 
							wid = dic.word2id['UNKNOWN']
						end
						datum[{id}] = wid
					end
				end

				data[#data+1] = datum
			end
		end
	end

	local ret = torch.IntTensor(1+2*window_size, #data)
	for i,datum in ipairs(data) do
		ret[{{},i}]:copy(datum)
	end
	--print(ret:size())
	
	-- swap
	local temp = ret[{{1,window_size},{}}]:clone()
	ret[{1,{}}]:copy(ret[{1+window_size,{}}])
	ret[{{2,window_size+1},{}}]:copy(temp)
	print(ret[{{},{1,20}}])

	-- save
	f = torch.DiskFile(output_path, 'w')
	f:binary()
	f:writeObject(ret)
	f:close()
end

if #arg == 4 then
	dic_emb_path = arg[1]
	context_path = arg[2]
	window_size = tonumber(arg[3])
	output_path	= arg[4]
	main()
else
	print("<dic_emb_path> <context_path> <window_size> <output_path>")
end
